import os
import PyPDF2
from docx import Document

# PDF 到 Word 转换
def pdf_to_word(pdf_path, word_path):
    pdf_reader = PyPDF2.PdfReader(pdf_path)
    doc = Document()
    for page in pdf_reader.pages:
        text = page.extract_text()
        if text:
            doc.add_paragraph(text)
    doc.save(word_path)

# 查找电脑上的 .pdf 文件
def find_pdf_files(directory):
    pdf_files = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.pdf'):
                pdf_files.append(os.path.join(root, file))
    return pdf_files

# 主函数
def main():
    directory = r'D:\软件安装包及其它'  
    pdf_files = find_pdf_files(directory)
    
    for pdf_file in pdf_files:
        word_file = pdf_file.replace('.pdf', '.docx')
        pdf_to_word(pdf_file, word_file)
        print(f"转换并保存了 {pdf_file} 到 {word_file}")

if __name__ == "__main__":
    main()